#!/usr/bin/python
#coding=utf-8

import sys
import os
import rados
import threading
import commands
import copy
import logging
import logging.config

import global_list
from command import *

class ReqDiskusg:
    #self.h=链接集群的handle
    def __init__(self,handle,dict,reqid,logger):
        self.h=handle
        self.act=dict['act']
        self.val=dict['val']
        self.reqid=int(reqid)
        self.logger=logger

    #线程处理函数
    def ThreadDealer(self):
        resp={}
        resp['act']=str(self.act)
        resp['type']='diskusage'
        resp['status']='running'

        #tname=threading.currentThread().getName()
        mtx=global_list.get_value('MUTEX')
        #加锁->获取结果列表->追加结果->更新列表->解锁
        mtx.acquire()
        res=global_list.get_value('RESULTS')
        res[int(self.reqid)]=copy.deepcopy(resp)
        global_list.set_value('RESULTS',res)
        mtx.release()

        #执行具体命令
        if self.act == 'getstats':
            ret=self.getstats()
            if False == ret.has_key('err'):
                resp.update(ret)
                resp['status']='success'
            else:
                resp['status']='failed'
        elif self.act == 'setnearfull':
            ret=self.setnearfull()
            if ret == False:
                resp['status']='failed'
            else:
                resp['status']='success'
        elif self.act == 'setfull':
            ret=self.setfull()
            if ret == False:
                resp['status']='failed'
            else:
                resp['status']='success'
        else:
            pass
 
        #更新结果
        mtx=global_list.get_value('MUTEX')
        mtx.acquire()
        res=global_list.get_value('RESULTS')
        res[int(self.reqid)]=copy.deepcopy(resp)
        global_list.set_value('RESULTS',res)
        mtx.release()

    #获取集群容量状态
    def getstats(self):
        respdict=dict()

        #获取集群状态
        cluster_stats=self.h.get_cluster_stats()
        respdict['size']=cluster_stats['kb']
        respdict['used']=cluster_stats['kb_used']
        respdict['avail']=cluster_stats['kb_avail']

        #获取全局nearfull和full
        pg_dump = CephClusterCommand(self.h, prefix='pg dump', strings='summary', format='json')
        if pg_dump.has_key('err') == True:
            self.logger.error("reqid:"+str(self.reqid)+" pg dump failed, error is "+pg_dump['err'])
            respdict['err']=pg_dump['err']
            return respdict
        result=pg_dump['result']
        respdict['nearfull_ratio']=result.get('near_full_ratio',0)
        respdict['full_ratio']=result.get('full_ratio',0)

        self.logger.info("reqid:"+str(self.reqid)+" success")
        return respdict

    #修改集群容量nearfull_ratio阈值
    def setnearfull(self):
        cmd="/usr/bin/ceph pg set_nearfull_ratio "+str(self.val)
        ret=os.system(cmd)
        if ret != 0:
            self.logger.error("reqid:"+str(self.reqid)+" pg set_nearfull_ratio failed")
            return False
        
        self.logger.info("reqid:"+str(self.reqid)+" success")
        return True

    #修改集群容量full_ratio阈值
    def setfull(self):
        cmd="/usr/bin/ceph pg set_full_ratio "+str(self.val)
        ret=os.system(cmd)
        if ret != 0:
            self.logger.error("reqid:"+str(self.reqid)+" pg set_full_ratio failed")
            return False

        self.logger.info("reqid:"+str(self.reqid)+" success")
        return True

    #析构
    def __del__(self):
        pass
